home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / XML / Parser / Simple.php
PHP Script  |  2004-10-01  |  8KB  |  280 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stephan Schmidt <schst@php-tools.net>                        |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Simple.php,v 1.3 2004/05/25 13:26:42 schst Exp $
  20.  
  21. /**
  22.  * Simple XML parser class.
  23.  *
  24.  * This class is a simplified version of XML_Parser.
  25.  * In most XML applications the real action is executed,
  26.  * when a closing tag is found.
  27.  *
  28.  * XML_Parser_Simple allows you to just implement one callback
  29.  * for each tag that will receive the tag with its attributes
  30.  * and CData
  31.  *
  32.  * @category XML
  33.  * @package XML_Parser
  34.  * @author  Stephan Schmidt <schst@php-tools.net>
  35.  */
  36.  
  37. /**
  38.  * built on XML_Parser
  39.  */
  40. require_once 'XML/Parser.php';
  41.  
  42. /**
  43.  * Simple XML parser class.
  44.  *
  45.  * This class is a simplified version of XML_Parser.
  46.  * In most XML applications the real action is executed,
  47.  * when a closing tag is found.
  48.  *
  49.  * XML_Parser_Simple allows you to just implement one callback
  50.  * for each tag that will receive the tag with its attributes
  51.  * and CData.
  52.  *
  53.  * <code>
  54.  * require_once '../Parser/Simple.php';
  55.  *
  56.  * class myParser extends XML_Parser_Simple
  57.  * {
  58.  *     function myParser()
  59.  *     {
  60.  *        $this->XML_Parser_Simple();
  61.  *      }
  62.  * 
  63.  *    function handleElement($name, $attribs, $data)
  64.  *     {
  65.  *         printf('handle %s<br>', $name);
  66.  *     }
  67.  * }
  68.  * 
  69.  * $p = &new myParser();
  70.  * 
  71.  * $result = $p->setInputFile('myDoc.xml');
  72.  * $result = $p->parse();
  73.  * </code>
  74.  *
  75.  * @category XML
  76.  * @package XML_Parser
  77.  * @author  Stephan Schmidt <schst@php-tools.net>
  78.  */
  79. class XML_Parser_Simple extends XML_Parser
  80. {
  81.    /**
  82.     * element stack
  83.     *
  84.     * @access   private
  85.     * @var      array
  86.     */
  87.     var $_elStack = array();
  88.  
  89.    /**
  90.     * all character data
  91.     *
  92.     * @access   private
  93.     * @var      array
  94.     */
  95.     var $_data = array();
  96.  
  97.    /**
  98.     * element depth
  99.     *
  100.     * @access   private
  101.     * @var      integer
  102.     */
  103.     var $_depth = 0;
  104.  
  105.     /**
  106.      * Creates an XML parser.
  107.      *
  108.      * This is needed for PHP4 compatibility, it will
  109.      * call the constructor, when a new instance is created.
  110.      *
  111.      * @param string $srcenc source charset encoding, use NULL (default) to use
  112.      *                       whatever the document specifies
  113.      * @param string $mode   how this parser object should work, "event" for
  114.      *                       handleElement(), "func" to have it call functions
  115.      *                       named after elements (handleElement_$name())
  116.      * @param string $tgenc  a valid target encoding
  117.      */
  118.     function XML_Parser_Simple($srcenc = null, $mode = 'event', $tgtenc = null)
  119.     {
  120.         $this->XML_Parser($srcenc, $mode, $tgtenc);
  121.     }
  122.  
  123.     /**
  124.      * inits the handlers
  125.      *
  126.      * @access  private
  127.      */
  128.     function _initHandlers()
  129.     {
  130.         if (!is_object($this->_handlerObj)) {
  131.             $this->_handlerObj = &$this;
  132.         }
  133.  
  134.         if ($this->mode != 'func' && $this->mode != 'event') {
  135.             return $this->raiseError('Unsupported mode given', XML_PARSER_ERROR_UNSUPPORTED_MODE);
  136.         }
  137.         xml_set_object($this->parser, $this);
  138.  
  139.         xml_set_element_handler($this->parser, 'startHandler', 'endHandler');
  140.  
  141.         /**
  142.          * set additional handlers for character data, entities, etc.
  143.          */
  144.         foreach ($this->handler as $xml_func => $method) {
  145.             if (method_exists($this, $method)) {
  146.                 $xml_func = 'xml_set_' . $xml_func;
  147.                 $xml_func($this->parser, $method);
  148.             }
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Reset the parser.
  154.      *
  155.      * This allows you to use one parser instance
  156.      * to parse multiple XML documents.
  157.      *
  158.      * @access   public
  159.      * @return   boolean|object     true on success, PEAR_Error otherwise
  160.      */
  161.     function reset()
  162.     {
  163.         $this->_elStack = array();
  164.         $this->_data    = array();
  165.         $this->_depth   = 0;
  166.         
  167.         $result = $this->_create();
  168.         if ($this->isError( $result )) {
  169.             return $result;
  170.         }
  171.     }
  172.  
  173.    /**
  174.     * start handler
  175.     *
  176.     * Pushes attributes and tagname onto a stack
  177.     *
  178.     * @access   private
  179.     * @final
  180.     * @param    resource    xml parser resource
  181.     * @param    string      element name
  182.     * @param    array       attributes
  183.     */
  184.     function startHandler($xp, $elem, &$attribs)
  185.     {
  186.         array_push($this->_elStack, array(
  187.                                             'name'    => $elem,
  188.                                             'attribs' => $attribs
  189.                                         )
  190.                   );
  191.         $this->_depth++;
  192.         $this->_data[$this->_depth] = '';
  193.     }
  194.  
  195.    /**
  196.     * end handler
  197.     *
  198.     * Pulls attributes and tagname from a stack
  199.     *
  200.     * @access   private
  201.     * @final
  202.     * @param    resource    xml parser resource
  203.     * @param    string      element name
  204.     */
  205.     function endHandler($xp, $elem)
  206.     {
  207.         $el   = array_pop($this->_elStack);
  208.         $data = $this->_data[$this->_depth];
  209.         $this->_depth--;
  210.  
  211.         switch ($this->mode) {
  212.             case 'event':
  213.                 $this->_handlerObj->handleElement($el['name'], $el['attribs'], $data);
  214.                 break;
  215.             case 'func':
  216.                 $func = 'handleElement_' . $elem;
  217.                 if (method_exists($this->_handlerObj, $func)) {
  218.                     call_user_func(array(&$this->_handlerObj, $func), $el['name'], $el['attribs'], $data);
  219.                 }
  220.                 break;
  221.         }
  222.     }
  223.  
  224.    /**
  225.     * handle character data
  226.     *
  227.     * @access   private
  228.     * @final
  229.     * @param    resource    xml parser resource
  230.     * @param    string      data
  231.     */
  232.     function cdataHandler($xp, $data)
  233.     {
  234.         $this->_data[$this->_depth] .= $data;
  235.     }
  236.  
  237.    /**
  238.     * handle a tag
  239.     *
  240.     * Implement this in your parser 
  241.     *
  242.     * @access   public
  243.     * @abstract
  244.     * @param    string      element name
  245.     * @param    array       attributes
  246.     * @param    string      character data
  247.     */
  248.     function handleElement($name, $attribs, $data)
  249.     {
  250.     }
  251.  
  252.    /**
  253.     * get the current tag depth
  254.     *
  255.     * The root tag is in depth 0.
  256.     *
  257.     * @access   public
  258.     * @return   integer
  259.     */
  260.     function getCurrentDepth()
  261.     {
  262.         return $this->_depth;
  263.     }
  264.  
  265.    /**
  266.     * add some string to the current ddata.
  267.     *
  268.     * This is commonly needed, when a document is parsed recursively.
  269.     *
  270.     * @access   public
  271.     * @param    string      data to add
  272.     * @return   void
  273.     */
  274.     function addToData( $data )
  275.     {
  276.         $this->_data[$this->_depth] .= $data;
  277.     }
  278. }
  279. ?>
  280.